package com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.ChangeStorageAddOperatorActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.ChangeStorageMaterielListActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.ChangeStorageResultActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageReceiveOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageDetailBatchActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.changeStorage.ChangeStorageListAdapter;
import com.inossem.warehousestorage_inossem.adapter.changeStorage.chuangyou.CYChangeStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageConfirmBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageMaterielListBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageConfirmBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageMaterialListBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStoragePurchaseOrderBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageAllocatingOverviewBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.changeStorage.ChangeStorageConfirmRequest;
import com.inossem.warehousestorage_inossem.request.changeStorage.chuangyou.CYChangeStorageMaterialListRequest;
import com.inossem.warehousestorage_inossem.request.changeStorage.chuangyou.CYChangeStorageSearchPurchaseOrderRequest;
import com.inossem.warehousestorage_inossem.request.inStorage.chuangyou.CYChangeStorageConfirmRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.CYChangeStorageUtils;
import com.inossem.warehousestorage_inossem.utils.ChangeStorageUtils;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import static com.inossem.warehousestorage_inossem.utils.ChangeStorageUtils.getChangeStorageBean;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/22 10:32
 * @email siyu.wang@inossem.com
 */
public class CYChangeStorageMaterielListActivity extends BaseActivity {

    private TextView title, nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private CYChangeStorageListAdapter adapter;

    private String mCondition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_change_storage_materiel_list);
//        SPUtils.removeChangeStorageMaterielList(CYChangeStorageMaterielListActivity.this);
        CYChangeStorageUtils.saveMaterialList(CYChangeStorageMaterielListActivity.this , new ArrayList<>());
        initView(CYChangeStorageMaterielListActivity.this);
        initData(CYChangeStorageMaterielListActivity.this);
    }
    @Override
    protected void onResumeImpl() {
       getData(CYChangeStorageMaterielListActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        materielList = (PullableListView) activity.findViewById(R.id.materielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.materielListRefresh);
    }
    private void initData(Activity activity) {
        title.setText(R.string.activity_in_storage_other_index_materiel_detail_material_list);
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_receive_result_back), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(CYChangeStorageMaterielListActivity.this, CYChangeStorageMaterielListActivity.this.getResources().getString(R.string.activity_in_storage_receive_overview_clear_prompt), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        finish();
                    }
                }).show();
            }
        });
        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (check(activity)) {
                    submitRequest(activity);
                }
            }
        });

        setmScanListener((String scanResult, int length) -> {
            mCondition = QRCodeUtils.DecryptQRCode(scanResult);
//            getData(activity, mCondition, mFactoryCode, mInventoryAddressCode, mBusinessType, mSpecialInventory);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);

    }
    private boolean check(Context context) {
        int confirmNumber = CYChangeStorageUtils.getChangeStorageMaterielNumber(context);
        if (confirmNumber == 0) {
            Utils.middleToast(context, getResources().getString(R.string.activity_change_storage_materiel_list_please_complete_at_least_one_line_item));
            return false;
        }
        return true;
    }
    private void addChangeStorageMaterielListData(Context context, List<ItemListBean> materialBeanList, boolean flag) {
        if (flag) {
            adapter = new CYChangeStorageListAdapter(context, mCondition,materialBeanList , new ChangeStorageMaterielListener() {
                @Override
                public void onNoDoubleClickListener(String condition ) {
                   mCondition = condition;
                   getData(context);
                }

                @Override
                public void onViewClickListener(String matId , String supplierCode) {
                   Intent intent = new Intent(CYChangeStorageMaterielListActivity.this , OutStorageDetailBatchActivity.class);
                   intent.putExtra(ChangeStorageExtra.MAT_ID , matId);
                    intent.putExtra(ChangeStorageExtra.SUPPLIER_CODE , supplierCode);
                    intent.putExtra(OutStorageExtra.TYPE, CYChangeStorageUtils.CHANGE_STORAGE_TYPE);
                   startActivity(intent);
                }
            });
            materielList.setAdapter(adapter);
        }
    }
    private List<String> getPurchaseOrderCodeList (Context context) {
        List<CYChangeStoragePurchaseOrderBean.PurchaseOrderBean> purchaseOrderBeanList = CYChangeStorageUtils.getPurchaseOrderList(context);
        List<String> purchaseOrderCodeList = new ArrayList<>();
        for (int i = 0; i < purchaseOrderBeanList.size(); i++) {
            purchaseOrderCodeList.add(purchaseOrderBeanList.get(i).getPurchase_orde_code());
        }
        return purchaseOrderCodeList;
    }
    private void getData (Context context ) {
        String changeStorageInformation = SPUtils.getChangeStorageInformation(context);
        Gson gson = new Gson();
        CYChangeStorageBean material = gson.fromJson(changeStorageInformation , CYChangeStorageBean.class);
        if (material.getMaterialBeanList() == null ||material.getMaterialBeanList().size() == 0) {
            CYChangeStorageMaterialListRequest.request(context, getPurchaseOrderCodeList(context), new GsonBeanListener<CYChangeStorageMaterialListBean>() {
                @Override
                public void onSuccess(CYChangeStorageMaterialListBean bean) {
                    Gson gson = new Gson();
                    String json = gson.toJson(bean, CYChangeStorageMaterialListBean.class);
                    addChangeStorageMaterielListData(context , bean.getBody() , true);
                    CYChangeStorageUtils.saveMaterialList(context , bean.getBody());
//                    SPUtils.saveChangeStorageMaterielList(context , json);
                }
            });
        }else {
            if (TextUtils.isEmpty(mCondition)) {
                addChangeStorageMaterielListData(context , material.getMaterialBeanList() , true);
            }else {
                addChangeStorageMaterielListData(context , CYChangeStorageUtils.getMaterialItem(context , mCondition) , true);
            }
        }

    }

    private void submitRequest (Context context) {
        CYChangeStorageConfirmRequestBean params = CYChangeStorageUtils.getChangeStorageConfirmRequestBean(context);
//        Gson gson = new Gson();
//        LogUtils.i("ChangeStorageConfirmRequestBean === " + gson.toJson(params , CYChangeStorageConfirmRequestBean.class));
        CYChangeStorageConfirmRequest.request(context, params, new GsonBeanListener<CYChangeStorageConfirmBean>() {
            @Override
            public void onSuccess(CYChangeStorageConfirmBean bean) {

                SPUtils.removeChangeStorageInformation(context);
                Intent intent = new Intent(context, ChangeStorageResultActivity.class);
                intent.putExtra(OutStorageExtra.MESSAGE, bean.getHead().getMsg());
                intent.putExtra(OutStorageExtra.CONFIRM_MESSAGE, "过账成功");
                startActivity(intent);
//                } else {
//                    if (!TextUtils.isEmpty(bean.getData().getDumpNumber())) {
//                        ChangeStorageUtils.updateDumpNumber(context, bean.getData().getDumpNumber());
//                    }
//                    Utils.middleToast(context, bean.getData().getMsg());
//                }
            }

            @Override
            public void onFailing(CYChangeStorageConfirmBean bean) {
                if (!TextUtils.isEmpty(bean.getBody().getStock_transport_id())) {
                    CYChangeStorageUtils.updateStockTransportId(context, bean.getBody().getStock_transport_id());
                }
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(context, bean.getHead().getMsg());
                }
            }
        });
    }
    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(CYChangeStorageMaterielListActivity.this, CYChangeStorageMaterielListActivity.this.getResources().getString(R.string.activity_in_storage_receive_overview_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                CYChangeStorageMaterielListActivity.super.onBackPressed();
            }
        }).show();
    }
    public interface ChangeStorageMaterielListener {
        void onNoDoubleClickListener(String condition);

        void onViewClickListener(String matId , String supplierCode);
    }
}
